﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace EasyAdmin.Web.EasySwagger
{
    public static class EasySwaggerExtensions
    {
        /// <summary>
        /// 添加Swagger服务
        /// </summary>
        /// <param name="builder"></param>
        public static void AddEasySwagger(this IServiceCollection services)
        {
            //添加swagger
            services.AddSwaggerGen(options =>
            {
                options.DocumentFilter<DefaultGroupDocumentFilter>(); //设置缺省组名的默认值为"default"


                EasyApiGroups.Groups.ForEach(group =>
                {

                    options.SwaggerDoc(group.Name, new OpenApiInfo
                    {
                        Version = "v1",
                        Title = group.Title,
                        Description = group.Description,
                        TermsOfService = new Uri("https://example.com/terms"),
                        Contact = new OpenApiContact
                        {
                            Name = "Andy.Zeng",
                            Email = "280948775@qq.com"
                        },
                        License = new OpenApiLicense
                        {
                            Name = "MIT",
                            Url = new Uri("https://www.cnblogs.com/vic-tory")
                        }
                    });
                });


                ////添加swagger服务
                //options.SwaggerDoc("v1", new OpenApiInfo
                //{
                //    Version = "v1",
                //    Title = "EasyAdmin API",
                //    Description = "用于测试使用",
                //    TermsOfService = new Uri("https://example.com/terms"),
                //    Contact = new OpenApiContact
                //    {
                //        Name = "Andy.Zeng",
                //        Email = "280948775@qq.com"
                //    },
                //    License = new OpenApiLicense
                //    {
                //        Name = "MIT",
                //        Url = new Uri("https://www.cnblogs.com/vic-tory")
                //    }
                //});               
                var xmlFilename = $"{Assembly.GetEntryAssembly().GetName().Name}.xml";
                options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));

                //配置Swagger的安全认证
                var securitySchema = new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.Http,
                    Scheme = "Bearer",
                    Reference = new OpenApiReference
                    {
                        Type = ReferenceType.SecurityScheme,
                        Id = "Bearer"
                    }
                };
                options.AddSecurityDefinition("Bearer", securitySchema);

                var securityRequirement = new OpenApiSecurityRequirement
                {
                    { securitySchema, new[] { "Bearer" } }
                };
                options.AddSecurityRequirement(securityRequirement);

            });

        }

        /// <summary>
        /// 开启添加Swagger服务
        /// </summary>
        /// <param name="app"></param>
        public static void UseEasySwagger(this WebApplication app)
        {
            //开发环境执行下列操作
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                EasyApiGroups.Groups.ForEach(group =>
                {
                    options.SwaggerEndpoint($"/swagger/{group.Name}/swagger.json", group.Title);
                });
                //options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
                options.RoutePrefix = "";
            });
        }
    }
}
